﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;
using System.Data;
using System.Data.OracleClient;

namespace TierApplication
{
    public partial class T_ACCOUNT
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="UserCode"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public T_ACCOUNT Login(string ACCOUNTID, string ACCOUNTPWD)
        {
            T_ACCOUNT info = null;
            StringBuilder Sql = new StringBuilder();
            Sql.Append("select ACCOUNTID,ACCOUNTNAME,ACCOUNTPWD,SCHOOLID,SNO,IDCARD,EMAIL,MOBILE,ROLEID,GENDER,BIRTHDAY,CONTAC_ADDR,POSTCODE,CURRENTPOINT,ISEMAILCONFIRM,GRADE ");
            Sql.Append(" FROM T_ACCOUNT ");
            Sql.Append(" where 1=1 ");
            Sql.Append(" and ACCOUNTID=:ACCOUNTID");
            Sql.Append(" and ACCOUNTPWD=:ACCOUNTPWD");
            OracleParameter[] parameters = {
					new OracleParameter("ACCOUNTID", OracleType.VarChar,20)
                    ,new OracleParameter("ACCOUNTPWD", OracleType.VarChar,90)};
            parameters[0].Value = ACCOUNTID;
            parameters[1].Value = ACCOUNTPWD;
            DataSet ds = DbHelperOra.Query(Sql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                info = new T_ACCOUNT();
                if (ds.Tables[0].Rows[0]["ACCOUNTID"] != null && ds.Tables[0].Rows[0]["ACCOUNTID"].ToString() != "")
                {
                    info.ACCOUNTID = ds.Tables[0].Rows[0]["ACCOUNTID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ACCOUNTNAME"] != null && ds.Tables[0].Rows[0]["ACCOUNTNAME"].ToString() != "")
                {
                    info.ACCOUNTNAME = ds.Tables[0].Rows[0]["ACCOUNTNAME"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ACCOUNTPWD"] != null && ds.Tables[0].Rows[0]["ACCOUNTPWD"].ToString() != "")
                {
                    info.ACCOUNTPWD = ds.Tables[0].Rows[0]["ACCOUNTPWD"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SCHOOLID"] != null && ds.Tables[0].Rows[0]["SCHOOLID"].ToString() != "")
                {
                    info.SCHOOLID = ds.Tables[0].Rows[0]["SCHOOLID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SNO"] != null && ds.Tables[0].Rows[0]["SNO"].ToString() != "")
                {
                    info.SNO = ds.Tables[0].Rows[0]["SNO"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IDCARD"] != null && ds.Tables[0].Rows[0]["IDCARD"].ToString() != "")
                {
                    info.IDCARD = ds.Tables[0].Rows[0]["IDCARD"].ToString();
                }
                if (ds.Tables[0].Rows[0]["EMAIL"] != null && ds.Tables[0].Rows[0]["EMAIL"].ToString() != "")
                {
                    info.EMAIL = ds.Tables[0].Rows[0]["EMAIL"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MOBILE"] != null && ds.Tables[0].Rows[0]["MOBILE"].ToString() != "")
                {
                    info.MOBILE = ds.Tables[0].Rows[0]["MOBILE"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ROLEID"] != null && ds.Tables[0].Rows[0]["ROLEID"].ToString() != "")
                {
                    info.ROLEID = ds.Tables[0].Rows[0]["ROLEID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["GENDER"] != null && ds.Tables[0].Rows[0]["GENDER"].ToString() != "")
                {
                    info.GENDER = ds.Tables[0].Rows[0]["GENDER"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BIRTHDAY"] != null && ds.Tables[0].Rows[0]["BIRTHDAY"].ToString() != "")
                {
                    info.BIRTHDAY = DateTime.Parse(ds.Tables[0].Rows[0]["BIRTHDAY"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CONTAC_ADDR"] != null && ds.Tables[0].Rows[0]["CONTAC_ADDR"].ToString() != "")
                {
                    info.CONTAC_ADDR = ds.Tables[0].Rows[0]["CONTAC_ADDR"].ToString();
                }
                if (ds.Tables[0].Rows[0]["POSTCODE"] != null && ds.Tables[0].Rows[0]["POSTCODE"].ToString() != "")
                {
                    info.POSTCODE = ds.Tables[0].Rows[0]["POSTCODE"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CURRENTPOINT"] != null && ds.Tables[0].Rows[0]["CURRENTPOINT"].ToString() != "")
                {
                    info.CURRENTPOINT = decimal.Parse(ds.Tables[0].Rows[0]["CURRENTPOINT"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ISEMAILCONFIRM"] != null && ds.Tables[0].Rows[0]["ISEMAILCONFIRM"].ToString() != "")
                {
                    info.ISEMAILCONFIRM = ds.Tables[0].Rows[0]["ISEMAILCONFIRM"].ToString();
                }
                if (ds.Tables[0].Rows[0]["GRADE"] != null && ds.Tables[0].Rows[0]["GRADE"].ToString() != "")
                {
                    info.GRADE = ds.Tables[0].Rows[0]["GRADE"].ToString();
                }
            }
            return info;
        }

        public DataSet GetAccount(string ACCOUNTID)
        {
            StringBuilder Sql = new StringBuilder();
            Sql.Append(" select ACCOUNTID,ACCOUNTNAME,ACCOUNTPWD,SCHOOLID,SNO,IDCARD,EMAIL,MOBILE,ROLEID,GENDER, ");
            Sql.Append(" BIRTHDAY,CONTAC_ADDR,POSTCODE,CURRENTPOINT,ISEMAILCONFIRM,GRADE ");
            Sql.Append(" ,PG_ENCRYPT_DECRYPT.DECRYPT_3KEY_MODE(ACCOUNTPWD,1) pwd  ");
            Sql.Append(" ,(select schoolshortname from t_school a where a.schoolid=t.SCHOOLID) SchoolName ");
            Sql.Append(" FROM T_ACCOUNT t");
            Sql.Append(" where 1=1 ");
            Sql.Append(" and ACCOUNTID=:ACCOUNTID");
            OracleParameter[] parameters = {
					new OracleParameter("ACCOUNTID", OracleType.VarChar,20)};
            parameters[0].Value = ACCOUNTID;
            return DbHelperOra.Query(Sql.ToString(), parameters);
        }

        public string GET_400HOTLINE()
        {
            StringBuilder Sql = new StringBuilder();
            Sql.Append(" Select GET_400HOTLINE() From dual ");
            DataSet ds = DbHelperOra.Query(Sql.ToString());
            if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count!=0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }

        public bool CheckAccout(string ACCOUNTID, string ACCOUNTPWD)
        {
            T_ACCOUNT info = null;
            StringBuilder Sql = new StringBuilder();
            Sql.Append("select * ");
            Sql.Append(" FROM T_ACCOUNT ");
            Sql.Append(" where 1=1 ");
            Sql.Append(" And  ACCOUNTID='" + ACCOUNTID + "'");
            Sql.Append(" And  ACCOUNTPWD=PG_ENCRYPT_DECRYPT.ENCRYPT_3KEY_MODE('" + ACCOUNTPWD + "',1)");
            DataSet ds = DbHelperOra.Query(Sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        public bool ModifyPassword(string ACCOUNTID, string ACCOUNTPWD)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_ACCOUNT set ");
            strSql.Append(" ACCOUNTPWD=PG_ENCRYPT_DECRYPT.ENCRYPT_3KEY_MODE('" + ACCOUNTPWD + "',1)");
            strSql.Append(" where ACCOUNTID='" + ACCOUNTID + "'");
            int rows = DbHelperOra.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
